﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace NotificationServer.DataAccessLayer.DAO
{
    public class DeviceDAO
    {
        private DbAccess _dbAccess;

        public DeviceDAO()
        {
            _dbAccess = new DbAccess();
        }

        public bool RegisterDevice(Guid guid, string name, string device_type, string registration_id)
        {            
            string query = string.Format("INSERT INTO Device(id, name, device_type, registration_id) VALUES(@id, @name, @device_type, @registration_id)");
            SqlParameter[] sqlParams = new SqlParameter[4];
            sqlParams[0] = new SqlParameter("@id", SqlDbType.UniqueIdentifier);
            sqlParams[0].Value = guid;
            sqlParams[1] = new SqlParameter("@name", SqlDbType.NVarChar);
            sqlParams[1].Value = name;
            sqlParams[2] = new SqlParameter("@device_type", SqlDbType.NVarChar);
            sqlParams[2].Value = device_type;
            sqlParams[3] = new SqlParameter("@registration_id", SqlDbType.NVarChar);
            sqlParams[3].Value = registration_id;
            
            return _dbAccess.InsertQuery(query, sqlParams);
        }

        public int UpdateDevice(Guid guid, string name, string registration_id)
        {
            string query = string.Format("UPDATE Device SET name=@name, registration_id=@registration_id WHERE id=@guid");
            SqlParameter[] sqlParams = new SqlParameter[3];
            sqlParams[0] = new SqlParameter("@guid", SqlDbType.UniqueIdentifier);
            sqlParams[0].Value = guid;
            sqlParams[1] = new SqlParameter("@name", SqlDbType.NVarChar);
            sqlParams[1].Value = name;
            sqlParams[2] = new SqlParameter("@registration_id", SqlDbType.NVarChar);
            sqlParams[2].Value = registration_id;

            return _dbAccess.UpdateQuery(query, sqlParams);
        }

        public int UnregisterDevice(Guid guid)
        {
            string query = string.Format("DELETE FROM Device WHERE id=@guid");
            SqlParameter[] sqlParams = new SqlParameter[1];
            sqlParams[0] = new SqlParameter("@guid", SqlDbType.UniqueIdentifier);
            sqlParams[0].Value = guid;

            return _dbAccess.DeleteQuery(query, sqlParams);
        }

        public DataTable GetDevices()
        {
            string query = string.Format("SELECT * FROM DEVICE");
            return _dbAccess.SelectQuery(query, null);
        }

        public DataTable GetRegistrationId(Guid deviceId)
        {
            string query = string.Format("SELECT registration_id FROM Device WHERE id=@id");
            SqlParameter[] sqlParams = new SqlParameter[1];
            sqlParams[0] = new SqlParameter("@id", SqlDbType.UniqueIdentifier);
            sqlParams[0].Value = deviceId;
            return _dbAccess.SelectQuery(query, sqlParams);
        }

        public DataTable SearchDevice(string name, string registration_id)
        {
            string query = string.Format("SELECT * FROM Device WHERE name=@name OR registration_id=@registration_id");
            SqlParameter[] sqlParams = new SqlParameter[2];            
            sqlParams[0] = new SqlParameter("@name", SqlDbType.NVarChar);
            sqlParams[0].Value = name;
            sqlParams[1] = new SqlParameter("@registration_id", SqlDbType.NVarChar);
            sqlParams[1].Value = registration_id;

            return _dbAccess.SelectQuery(query, sqlParams);
        }

        public DataTable SearchDevice(Guid guid, string name, string registration_id)
        {
            string query = string.Format("SELECT * FROM Device WHERE id=@id OR name=@name OR registration_id=@registration_id");
            SqlParameter[] sqlParams = new SqlParameter[3];
            sqlParams[0] = new SqlParameter("@id", SqlDbType.UniqueIdentifier);
            sqlParams[0].Value = guid;
            sqlParams[1] = new SqlParameter("@name", SqlDbType.NVarChar);
            sqlParams[1].Value = name;            
            sqlParams[2] = new SqlParameter("@registration_id", SqlDbType.NVarChar);
            sqlParams[2].Value = registration_id;

            return _dbAccess.SelectQuery(query, sqlParams);
        }
    }
}